<?php
/*
 * Author :basanas &pandiam
 */
session_start();

if ( !isset( $_SESSION['uid'] ) || $_SESSION['uid'] == -1 )
{
    UI_registerError( 'msg_warning', "You are not logged in." );
    header( 'Location: login.php' );
}

require_once ( '../BL/BL.php' );
require_once ( '../frameworks/objects/category.php' );

$arr = Array();

if ( $_GET['op'] == "add" )
{
    echo 'Use the <img src="images/addEntity.png"/> button to insert entities to a category';
}
else if ( $_GET['op'] == "delete" )
{
    echo 'Use the <img src="images/deleteContent.png"/> button to delete entities from a category';
}
else if ( $_GET['op'] == "edit" )
{
    echo 'Use the <img src="images/editEntity.png"/> button to edit entities in a category';
}
else if ( $_GET['op'] == "catEdit" )
{
    echo 'Use the <img src="images/editCategory.png"/> button to edit a category';
}
else if ( $_GET['op'] == "catDelete" )
{
    echo 'Use the <img src="images/deleteContent.png"/> button to delete a category';
}
else if($_GET['op'] == "enab" && isset($_SESSION['Admin']) && $_SESSION['Admin']!=-1 )
{
    echo ' Use the <img src="images/enable.png"/> buttons to enable users';
}
else if($_GET['op'] == "disab" && isset($_SESSION['Admin']) && $_SESSION['Admin']!=-1 )
{
    echo ' Use the <img src="images/disable.png"/> buttons to disable users';
}
else if($_GET['op'] == "pend" && isset($_SESSION['Admin']) && $_SESSION['Admin']!=-1 )
{
    echo ' Use the <img src="images/enable.png"/> buttons to enable a pending category';
}
else if($_GET['op'] == "rest" && isset($_SESSION['Admin']) && $_SESSION['Admin']!=-1 )
{
    echo ' Use the <img src="images/enable.png"/> buttons to restore a deleted category';
}
else if($_GET['op'] == "allCats" && isset($_SESSION['Admin']) && $_SESSION['Admin']!=-1 )
{
    echo ' Use the <img src="images/editCategory.png"/> buttons to edit a category';
}
else if($_GET['op'] == "deleteCats" && isset($_SESSION['Admin']) && $_SESSION['Admin']!=-1 )
{
    echo ' Use the <img src="images/deleteContent.png"/> buttons to edit a category';
}
else
{
    header( 'location: profile.php?page=categories' );
    exit(0);
}

echo '<div style="margin-left: 30%;">';

if($_GET['op'] == "enab" || $_GET['op'] == "disab")
    echo '<h2>System Users</h2>';
else
    echo '<h2>My Categories</h2>';


echo '  <table class="blockBlue" width="50%">';

if ( $_GET['op'] == "add" )
{
    $arr = BL_getUserCategories($_SESSION['uid']);
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
            <tr>
               <td class='tC'> <?php echo $arr[$i][1] ?> </td>
               <td class='tC'><a href="insertEntity.php?id=<?php echo $arr[$i][0]?> "><img src="images/addEntity.png" /></a></td>
            </tr>
<?php
    }
}
else if ( $_GET['op'] == "delete" )
{
    $arr = BL_getOwnerModCategories($_SESSION['uid']);
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
            <tr>
               <td class='tC'> <?php echo $arr[$i][1]?> </td>
               <td class='tC'><a href="deleteEntity.php?id=<?php echo $arr[$i][0]?> "><img src="images/deleteContent.png" /></a></td>
            </tr>
<?php
    }
}
else if ( $_GET['op'] == "edit" )
{
    $arr = BL_getOwnerModCategories($_SESSION['uid']);
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
            <tr>
               <td class='tC'> <?php echo $arr[$i][1] ?> </td>
               <td class='tC'><a href="selectEntity.php?id=<?php echo $arr[$i][0] ?> "><img src="images/editEntity.png"/></a></td>
            </tr>
<?php
    }
}
else if ( $_GET['op'] == "catEdit" )
{
    $arr = BL_getOwnerModCategories($_SESSION['uid']);
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
            <tr>
              <td class='tC'> <?php echo $arr[$i][1]?> </td>
              <td class='tC'><a href="editCategory.php?id=<?php echo $arr[$i][0]?> "><img src="images/editCategory.png" /></a></td>
            </tr>
<?php
    }
}
else if ( $_GET['op'] == "catDelete" )
{
    $arr = BL_get_categories_with_owner($_SESSION['uid']);
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
            <tr>
               <td class='tC'> <?php echo $arr[$i][1]?> </td>
               <td class='tC'><a href="deleteCategory.php?id=<?php echo $arr[$i][0]?> "><img src="images/deleteContent.png"/></a></td>
            </tr>
<?php
    }
}
else if($_GET['op'] == "enab" && $_SESSION['Admin']!=-1){
    $arr = BL_getDisabledUsers();
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
        <tr>
                <td class='tC'> <?php echo $arr[$i] ?> </td>
                <td class='tC'>
                <a href="enableUser.php?name=<?php echo $arr[$i]?> "><img src="images/enable.png" /></a>
                </td>
        </tr>    
<?php
    }
}
else if($_GET['op'] == "disab" && $_SESSION['Admin']!=-1){
    $arr = BL_getEnabledUsers();
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
        <tr>
                <td class='tC'> <?php echo $arr[$i] ?> </td>
                <td class='tC'>
                <a href="disableUser.php?name=<?php echo $arr[$i]?> "><img src="images/disable.png" /></a>
                </td>
        </tr>    
<?php
    }
}
else if($_GET['op'] == "pend" && $_SESSION['Admin']!=-1){
    $arr = BL_getPendingCategories();
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
        <tr>
                <td class='tC'> <?php echo $arr[$i]->GetName() ?> </td>
                <td class='tC'>
                <a href="enableCategory.php?id=<?php echo $arr[$i]->GetId()?> "><img src="images/enable.png" /></a>
                </td>
        </tr>
<?php
    }
}
else if($_GET['op'] == "rest" && $_SESSION['Admin']!=-1){
    $arr = BL_getDeletedCategories();
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
        <tr>
                <td class='tC'> <?php echo $arr[$i][1] ?> </td>
                <td class='tC'>
                <a href="restoreCategory.php?id=<?php echo $arr[$i][0]?> "><img src="images/enable.png" /></a>
                </td>
        </tr>
<?php
    }
}
else if($_GET['op'] == "allCats" && $_SESSION['Admin']!=-1){
    $arr = BL_getAllCategories();
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
        <tr>
                <td class='tC'> <?php echo $arr[$i][1] ?> </td>
                <td class='tC'>
                <a href="editCategory.php?id=<?php echo $arr[$i][0]?> "> <img src="images/editCategory.png" /></a>
                </td>
        </tr>
<?php
    }
}else if($_GET['op'] == "deleteCats" && $_SESSION['Admin']!=-1){
    $arr = BL_getAllCategories();
    for ( $i = 0; $i < count( $arr ); $i++ )
    {
?>
        <tr>
                <td class='tC'> <?php echo $arr[$i][1] ?> </td>
                <td class='tC'>
                <a href="deleteCategory.php?id=<?php echo $arr[$i][0]?> "> <img src="images/deleteContent.png" /></a>
                </td>
        </tr>
<?php
    }
}
?>
    </table>
</div>